<?php

/**
 * 兑奖接口
 */

class Api_exchange_mdl extends Base_Controller
{
	
	const TBL_LOTTERY = 'cj_lottery';
	
	public function __construct()
	{
		parent::__construct();
		$this->load->model('login_mdl','login');
	}
	
	
	/**
	 * ids
	 * Enter description here ...
	 * @param unknown_type $data
	 */
	public function outputdata($data)
	{
		header('Content-Type:text/html; charset=utf-8', false);
		$reward = array();
		
			//检查参数
		if(empty($data['REWARDID']) || empty($data['USERID']) || empty($data['RNUM']) || empty($data['PRODUCTID']) || empty($data['EMAIL'])){
			
			$tmp['TAG'] = $data['TAG'];
			$tmp['STATUS'] = 1;
			$tmp['PRODUCTNAME'] = '';
			$tmp['USERINFO'] = '';
			$tmp['ERRORMESSAGE'] = 'INFO ERROR';
			echo json_encode($tmp);
			exit;
		
		}
		//验证登陆
		if(!$this->login->_checklogin($data['USERID'],$data['EMAIL'],$data['TOKEN'])) {
			
			$tmp['TAG'] = $data['TAG'];
			$tmp['STATUS'] = 10001;
			$tmp['PRODUCTNAME'] = '';
			$tmp['USERINFO'] = '';
			$tmp['ERRORMESSAGE'] = 'NO LOGIN';
			echo json_encode($tmp);
			exit;
			
		}
		
		$reward = $this->check($data['USERID'],$data['EMAIL'],$data['REWARDID'],$data['PRODUCTID'],$data['RNUM']);

		if(!empty($reward))
		{

			if(empty($reward[0]['duijiang']))
			{
					if($this->changelottery($reward[0]['id']))
					{
						$logdata['uid']       = $reward[0]['uid'];
						$logdata['rewardid']  = $reward[0]['rewardid'];
						$logdata['rnum']      = $reward[0]['rnum'];
						$logdata['productid'] = $reward[0]['productid'];
						
						if(!empty($data['COUNTRY']) && !empty($data['STATE']) && !empty($data['CITY']) && !empty($data['STREET'])){
								
							$userinfo['txaddress'] = $data['COUNTRY'].'-'.$data['STATE'].'-'.$data['CITY'].'-'.$data['STREET'];
							$this->changeuserinfo($data['USERID'],$userinfo);
							$logdata['address']   = $data['COUNTRY'].'-'.$data['STATE'].'-'.$data['CITY'].'-'.$data['STREET'];
								
						}
												
						$logdata['lotteryid'] = $reward[0]['id'];
						$logdata['states']    = isset($data['STATE']) ? $data['STATE'] : '';
						$logdata['country']   = isset($data['COUNTRY']) ? $data['COUNTRY'] : '';
						$logdata['street']    = isset($data['STREET']) ? $data['STREET'] : '';
						$logdata['recvname']  = isset($data['RECVNAME']) ? $data['RECVNAME']: '';
						$logdata['zip']       = isset($data['ZIP']) ? $data['ZIP'] : '';
						$logdata['city']      = isset($data['CITY']) ? $data['CITY'] : '';
						$logdata['mobile'] = empty($data['MOBILE']) ? 0 : $data['MOBILE'];
						
						$logdata['createtime'] = time();
						$nowgold = 0;
						//根据不同奖品不同处理
						if( $reward[0]['ptype'] == 1 )
						{
							$nowgold = (int)$reward[0]['txgoldnum'];
							$exchangestate = 1;
							//修改用户资料
							!empty($data['STATE']) ? $userinfo['state'] = $data['STATE'] : '';
							if(!empty($data['COUNTRY']) && !empty($data['STATE']) && !empty($data['CITY']) && !empty($data['STREET'])){
								
								$userinfo['txaddress'] = $data['COUNTRY'].'-'.$data['STATE'].'-'.$data['CITY'].'-'.$data['STREET'];
								$this->changeuserinfo($data['USERID'],$userinfo);
								
							}
												
						}elseif( $reward[0]['ptype'] == 2 || $reward[0]['ptype'] == 3 ){
							$exchangestate = 3;
							if($reward[0]['ptype'] == 3){
								
								$nowgold = (int)$reward[0]['txgoldnum']+(int)$reward[0]['pprice'];
							}
							
							$this->changegoal($data['USERID'],$nowgold);
						}
						$logdata['exchangestate'] = $exchangestate;
						
						//添加兑奖数据
						$this->addlog($logdata);
					$tmp['TAG'] = $data['TAG'];
						$tmp['STATUS'] = 0;
						$tmp['PRODUCTNAME'] = $reward[0]['productname'];
						$tmp['USERINFO'] = array('USERID'=>(int)$reward[0]['uid'],'GOLD'=>$nowgold);
						$tmp['ERRORMESSAGE'] = null;
					}
					else 
					{
						//系统错误
						$tmp['TAG'] = $data['TAG'];
						$tmp['STATUS'] = 1;
						$tmp['PRODUCTNAME'] = null;
						$tmp['USERINFO'] = null;
						$tmp['ERRORMESSAGE'] = 'system error';
					}
				
			}
			else 
			{
					//已经兑奖
					$tmp['TAG'] = $data['TAG'];
					$tmp['STATUS'] = 1;
					$tmp['PRODUCTNAME'] = null;
					$tmp['USERINFO'] = null;
					$tmp['ERRORMESSAGE'] = 'haved';
			}

		}
		else 
		{
			//不存在
				$tmp['TAG'] = $data['TAG'];
				$tmp['STATUS'] = 1;
				$tmp['PRODUCTNAME'] = null;
				$tmp['USERINFO'] = null;
				$tmp['ERRORMESSAGE'] = 'none';
		}
		echo json_encode($tmp);
		
	}
	
	/**
	 * 
	 * 判断是否中奖
	 * @param int $uid
	 * @param string $username
	 * @param int $productid
	 * @param int $rewardid
	 * @package int $rnum
	 */
	private function check($uid,$username,$rewardid,$productid,$rnum)
	{
		$lottery = $this->db->select('lottery.id,lottery.uid,lottery.rewardid,lottery.rnum,product.productname,lottery.duijiang,lottery.stockid,
								product.id as productid,product.ptype,product.pprice,
								user.txaddress,user.txgoldnum,user.uid'
							)
						->where('lottery.rewardid',$rewardid)
						->where('lottery.rnum',$rnum)
						->where('lottery.uid',$uid)
						->where('lottery.stockid',$productid)
						->join( 'cj_products as product','product.id=lottery.productid' )
						->join( 'cj_users as user','user.uid=lottery.uid' )
						->from( self::TBL_LOTTERY.' as lottery' )
						->get()->result_array();
						
		return $lottery;
	}
	
	/**
	 * 更新lottert表
	 */
	
	private function changelottery($id)
	{
	
		$data['duijiang'] = 1;
		$this->db->where('id', $id);
		$this->db->update(self::TBL_LOTTERY, $data);
		return $this->db->affected_rows();
	}
	
	/**
	 * 体检信息
	 */
	
	private function addlog($data)
	{	
		return $this->db->insert('cj_exchange',$data);
	}
	
	
	/**
	 * 改变用户金币
	 */
	
	private function changegoal($userid,$goal)
	{
		$data['txgoldnum'] = intval($goal);
		$this->db->where('uid', intval($userid));
		$this->db->update('cj_users', $data);
		
		return $this->db->affected_rows();
	}
	
	/**
	 * 改变用户信息
	 */
	
	private function changeuserinfo($userid,$data)
	{
	
		$this->db->where('uid', intval($userid));
		$this->db->update('cj_users', $data);
		
		return $this->db->affected_rows();
	}
	
}